<template>
  <div class="login">

    <div class="heat_title"><img src="../assets/logo/logo.png" alt=""><span>城市地下谛听系统</span>
          <div class="heat_title_yuan"></div>
    </div>


    <div class="login_11">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code"  v-if="captchaOnOff"  style="padding-bottom: 0; margin-bottom: 0;">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 60%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox
          v-model="loginForm.rememberMe"

          class="checkboxD"
          >记住密码</el-checkbox
        >
        <el-form-item style="width: 85%; margin-left: 6%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100% ; background-image: linear-gradient(#e66465, #9198e5)}"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'"
              >立即注册</router-link
            >
          </div>
        </el-form-item>
      </el-form>
      <!--  底部  -->
      <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push( "/" ).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.login {
  height: 100%;
  background-image: url("../assets/img/lg_bg_001.png");
  overflow: hidden;
  background-size: cover;
  display: block;
}
.heat_title {
  width: 100%;
  text-align: center;
  height: 90px;
  font-size: 54px;
  font-family: PingFang SC-Semibold, PingFang SC;
  font-weight: 600;
  color: #ffffff;
  line-height: 80px;
  margin-top: 117px;
  letter-spacing: 13px;
  margin-bottom: 98px;
}
.heat_title img{
  display: inline-block;
  padding-right:30px;
}



@media (max-height: 900px) {
.login_11 {height: 45vh; }
.heat_title { font-size: 36px;}
.login-form { padding: 75px 50px 15px 80px !important;}

.login .el-input__inner{    height: 36px !important;line-height: 36px !important; }
.login .checkboxD {margin: 8px 0px 5px 0px !important; color: #9efbff}

}
@media (max-height: 800px) {
.login_11 {height: 50vh; margin: auto 25%; }
.login-form { padding: 70px 50px 15px 80px !important;}
.login .el-input__inner{    height: 32px !important;line-height: 32px !important; }
.login .el-button{ height: 32px !important; line-height: 32px !important;}
.login .el-button--medium {padding: 0px 20px;font-size: 12px;}
.login .el-form-item {margin-bottom: 2px !important; }
.login .checkboxD {margin: 6px 0px 5px 0px !important; color: #9efbff}

}

@media (max-height: 798px) {
.login_11 {height: 50vh; margin: auto 25%; }
.login-form { padding: 80px 50px 30px 80px !important;}
.login .el-input__inner{    height: 30px !important;line-height: 30px !important; }
.login .el-button{ height: 30px !important; line-height: 30px !important;}
.login .el-button--medium {padding: 0px 20px;font-size: 12px;}
.login .el-form-item {margin-bottom: 12px !important; }
.login .checkboxD {margin: 8px 0px 6px 0px !important; color: #9efbff}

}
.heat_title_yuan{
  width: 26%;
  height: 3px;
  margin: 1px 39%
  1px 39%;
       background-image: linear-gradient(#ACE6FF, #ff000000);
border-radius: 50%;
}

.login_11 {
  height: 40vh;
  width: 30%;
  margin: auto 35%;
  position: absolute;
}

.login-form {
  background: url("../assets/img/lg_bg_003.png") no-repeat;
  overflow: hidden;
  background-size: 100% 100%;
  height: 100%;
  padding: 90px 50px 8px 80px;


 .login  .el-input {
    height: 38px;
    input {
      height: 38px;
      color: #fff;
     background-image: radial-gradient(#072F88, #056CB0);
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
    color: #9BFDFF;

  }
}
.login .el-input__inner {
    -webkit-appearance: none;
    background-color: #062873;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #fff;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: 0;
    // padding: 0 15px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
}
.login .checkboxD {
      margin: 10px 0px 15px 0px; color: #9efbff
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;

}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login .el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login .el-button--primary {
    background-image: linear-gradient(#072F88, #056CB0)
}
.login-code-img {
  height: 38px;
}
</style>
